<?php

namespace app\common\model;

use think\Model;
use think\Db;
use think\Loader;
use extend\Tree;

class CategoryModel extends Model
{
  	protected $pk = 'id';
  	protected $table = 'basic_category';
    protected $categoryPage = [
        '0' => ''
    ];

  	public function getCategoryList($params = []){
        $curPage = 1;
        $pageSize = 15;
        $where['status'] = ['neq', 111];

        $totalRow = self::where($where)->count();
        $data = self::where($where)->select();
        $data = empty($data) ? [] : collection($data)->toArray();

        return [
            'pageSize' => $pageSize,
            'totalRow' => $totalRow,
            'curPage' => $curPage,
            'data' => $data
        ];
  	}

    public function getAllCategoryData(){
        $where = ['status'=>['neq',111]];
        $data = self::where($where)->select();
        return empty($data) ? [] : collection($data)->toArray();
    }

    public function getAllCategory(){
        $menuList = $this->getAllCategoryData();
        Loader::import('Tree', EXTEND_PATH);
        $Tree = new Tree();
        $Tree->init($menuList, 'category_name');
        $treelist = $Tree->get_tree_list();

        return $treelist;
    }

    //获取单个用户
    public function getCategoryById($id){
        $user = Db::table($this->table)->find($id);
        return empty($user) ? [] : collection($user)->toArray();
    }

    //批量改变状态
    public function changeStatus($ids, $status)
    {
        return self::where("id", "in", $ids)->update([
            'status' => $status,
            'update_time' => time()
        ]);
    }

    //删除
    public function deleteall($id)
    {
        if (!is_array($id)) {
            $id = [$id];
        }

        return self::where("id", "in", $id)->update([
            'status' => 111,
            'update_time' => time()
        ]);
    }

    //判断该菜单是否有子分类
    public function isChildren($id)
    {
        return self::where(["parent_id"=>$id,'status'=>['neq',111]])->count() > 0 ? true : false;
    }
}
